var t = require;
var e = module;
var o = exports;
var a,
    i =
        (this && this.__extends) ||
        ((a = function (t, e) {
            return (a =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (t, e) {
                        t.__proto__ = e;
                    }) ||
                function (t, e) {
                    for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
                })(t, e);
        }),
        function (t, e) {
            a(t, e);
            function o() {
                this.constructor = t;
            }
            t.prototype = null === e ? Object.create(e) : ((o.prototype = e.prototype), new o());
        }),
    n =
        (this && this.__decorate) ||
        function (t, e, o, a) {
            var i,
                n = arguments.length,
                r = n < 3 ? e : null === a ? (a = Object.getOwnPropertyDescriptor(e, o)) : a;
            if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(t, e, o, a);
            else
                for (var s = t.length - 1; s >= 0; s--)
                    (i = t[s]) && (r = (n < 3 ? i(r) : n > 3 ? i(e, o, r) : i(e, o)) || r);
            return n > 3 && r && Object.defineProperty(e, o, r), r;
        };
Object.defineProperty(o, "__esModule", {value: !0});
o.BezierPart = void 0;
var r = t("CreatePath"),
    s = cc._decorator,
    c = s.ccclass,
    p = s.property,
    l = (s.menu, s.executeInEditMode),
    d = (function () {
        function t(t, e, o, a) {
            this.posArray = [];
            this.lenght = null;
            this.startPos = t;
            this.cPos1 = e;
            this.cPos2 = o;
            this.endPos = a;
            this.lenght = this.getBezierLen();
        }
        t.prototype.createPosArray = function (t) {
            for (var e = Math.floor(this.lenght / t), o = 1 / e, a = o; a <= 1; a += o) {
                var i = this.bezier(this.startPos.x, this.cPos1.x, this.cPos2.x, this.endPos.x, a),
                    n = this.bezier(this.startPos.y, this.cPos1.y, this.cPos2.y, this.endPos.y, a);
                this.posArray.push(cc.v2(i, n));
            }
            this.posArray.length < e && this.posArray.push(this.endPos);
        };
        t.prototype.getBezierLen = function (t) {
            void 0 === t && (t = 20);
            var e,
                o,
                a = 0,
                i = cc.v2(0, 0);
            for (e = 0; e <= 1; e += 0.05) {
                var n = this.bezier(this.startPos.x, this.cPos1.x, this.cPos2.x, this.endPos.x, e),
                    r = this.bezier(this.startPos.y, this.cPos1.y, this.cPos2.y, this.endPos.y, e);
                a += (o = cc.v2(n, r)).sub(i).mag();
                i = o;
            }
            return a;
        };
        t.prototype.bezier = function (t, e, o, a, i) {
            return (
                t * Math.pow(1 - i, 3) + 3 * e * i * Math.pow(1 - i, 2) + 3 * o * i * i * (1 - i) + a * Math.pow(i, 3)
            );
        };
        return t;
    })();
o.BezierPart = d;
var u = (function (t) {
    i(e, t);
    function e() {
        var e = (null !== t && t.apply(this, arguments)) || this;
        e.lvIndex = 0;
        e.tabCount = 10;
        e.devCount = 10;
        e.tabA = 0;
        e.tabB = 0;
        e.saveMap = null;
        e.animalClip = null;
        return e;
    }
    Object.defineProperty(e.prototype, "saveData", {
        get: function () {
            return !1;
        },
        set: function () {
            console.log("EditorCom ----------------- 11111");
            this.save();
        },
        enumerable: !1,
        configurable: !0
    });
    e.prototype.udpataMap = function () {};
    e.prototype.onLoad = function () {
        this.udpataMap();
    };
    e.prototype.getAnimalPath = function (t, e) {
        console.log("EditAnimailPath ----------------- roadKey", e);
        var o = t.curveData.paths[e];
        if (!o) return [];
        var a = o.props.position,
            i = this.getBezierPartArray(a);
        return this._getPath(i);
    };
    e.prototype._getPath = function (t) {
        for (var e, o = [], a = 0; a < t.length; a++) {
            (e = t[a]).createPosArray(16);
            o = o.concat(e.posArray);
        }
        return o;
    };
    e.prototype.getBezierPartArray = function (t) {
        for (var e = [], o = 0; o < t.length - 1; o++) {
            var a = this.createBezierPartArray(t[o], t[o + 1]);
            e = e.concat(a);
        }
        return e;
    };
    e.prototype.createBezierPartArray = function (t, e) {
        var o,
            a,
            i,
            n,
            r,
            s,
            c = [],
            p = t.motionPath;
        o = cc.v2(t.value[0], t.value[1]);
        s = p[0];
        a = i = cc.v2(s[2], s[3]);
        n = cc.v2(s[0], s[1]);
        c.push(new d(o, a, i, n));
        for (var l = 0; l < p.length - 1; l++) {
            r = p[l];
            s = p[l + 1];
            o = cc.v2(r[0], r[1]);
            a = cc.v2(r[4], r[5]);
            i = cc.v2(s[2], s[3]);
            n = cc.v2(s[0], s[1]);
            c.push(new d(o, a, i, n));
        }
        r = p[p.length - 1];
        o = cc.v2(r[0], r[1]);
        a = i = cc.v2(r[4], r[5]);
        n = cc.v2(e.value[0], e.value[1]);
        c.push(new d(o, a, i, n));
        return c;
    };
    e.prototype.getNpcToDev = function () {
        for (var t = [], e = 0; e < this.devCount; e++) {
            var o = "dev" + (e + 1),
                a = this.node.getChildByName(o);
            if (a) {
                var i = a.getComponent(r.default);
                i && t.push(i);
            }
        }
        return t;
    };
    e.prototype.getNpcToTab = function () {
        for (var t = [], e = 0; e < this.tabCount; e++) {
            var o = "npctab" + (e + 1),
                a = this.node.getChildByName(o);
            if (a) {
                var i = a.getComponent(r.default);
                i && t.push(i);
            }
        }
        return t;
    };
    e.prototype.getRoadInTab = function () {
        for (var t = [], e = 0; e < this.tabCount; e++) {
            var o = "tab" + (e + 1),
                a = this.node.getChildByName(o);
            if (a) {
                var i = a.getComponent(r.default);
                i && t.push(i);
            }
        }
        return t;
    };
    e.prototype.getRoadOutTab = function () {
        for (var t = [], e = 0; e < this.tabCount; e++) {
            var o = "tabe" + (e + 1),
                a = this.node.getChildByName(o);
            if (a) {
                var i = a.getComponent(r.default);
                i && t.push(i);
            }
        }
        return t;
    };
    e.prototype.getInflectionPoint = function () {
        var t = this.node.getChildByName("inflectionPoint");
        if (t) {
            var e = t.getComponent(r.default);
            if (e) {
                for (var o = [], a = e.upataPos(), i = 0; i < a.length; i++) o.push({x: a[i].x, y: a[i].y});
                return o;
            }
        }
        return [];
    };
    e.prototype.getParseData = function () {
        var t = null,
            e = this.getNpcPath2Path(),
            o = this.getNpcToDev(),
            a = this.getNpcToTab(),
            i = this.getRoadInTab(),
            n = this.getRoadOutTab(),
            s = [],
            c = [],
            p = [];
        if (e && e.length > 0) {
            for (var l = 0; l < this.tabA; l++) {
                var d = "pathe" + (l + 1);
                (u = this.node.getChildByName(d)) && (h = u.getComponent(r.default)) && s.push(h);
            }
            for (l = 0; l < this.tabB; l++) {
                d = "path" + (l + 1);
                (u = this.node.getChildByName(d)) && (h = u.getComponent(r.default)) && c.push(h);
            }
            for (l = 0; l < this.tabB; l++) {
                var u;
                d = "pathb" + (l + 1);
                if ((u = this.node.getChildByName(d))) {
                    var h;
                    (h = u.getComponent(r.default)) && p.push(h);
                }
            }
            (t = {}).main = this.parseListToObj(e);
            t.path1 = this.getObjParse(s);
            t.path2 = this.getObjParse(c);
            t.pathb = this.getObjParse(p);
        }
        console.log("主路线进行断点 npcRoad");
        var f = this.getMainNpcPath();
        console.log("主路线断点waitCook");
        var y = this.getwaitCookNpcPath();
        console.log("主路线断点waitCook完成");
        console.log("顾客进入进行断点 roadIn");
        var v = this.getMainInPath();
        console.log("顾客进入完成");
        console.log("顾客退出进行断点 roadout");
        var g = this.getMainOutPath(),
            m = this.getObjParse(o),
            _ = this.getObjParse(a),
            C = this.getObjParse(i),
            b = this.getRoadDir(i),
            I = this.getObjParse(n);
        return {
            mainPath: (f = this.parseListToObj(f)),
            roadInPath: (v = this.parseListToObj(v)),
            roadOutPath: (g = this.parseListToObj(g)),
            waitCook: (y = this.parseListToObj(y)),
            tabRoadOutPath: I,
            tabRoadInPath: C,
            roadRoadDir: b,
            devNpcPath: m,
            tabNpcPath: _,
            centerData: t
        };
    };
    e.prototype.getObjParse = function (t) {
        for (var e = {}, o = 0; o < t.length; o++) e[o] = this.parseListToObj(t[o].upataPos());
        return e;
    };
    e.prototype.getRoadDir = function (t) {
        for (var e = {}, o = 0; o < t.length; o++) "1" != t[o].dirIndex && (e[o] = t[o].dirIndex);
        return e;
    };
    e.prototype.parseListToObj = function (t) {
        for (var e = [], o = 0; o < t.length; o++) e.push({x: t[o].x, y: t[o].y});
        return e;
    };
    e.prototype.getParseData2 = function () {
        var t = {},
            e = {},
            o = null,
            a = this.getNpcPath2Path(),
            i = this.getNpcToDev(),
            n = this.getNpcToTab(),
            s = this.getRoadInTab(),
            c = this.getRoadOutTab(),
            p = [],
            l = [];
        if (a && a.length > 0) {
            for (var d = 0; d < this.tabA; d++) {
                var u = "pathe" + (d + 1);
                (h = this.node.getChildByName(u)) && (f = h.getComponent(r.default)) && p.push(f);
            }
            for (d = 0; d < this.tabB; d++) {
                var h;
                u = "path" + (d + 1);
                if ((h = this.node.getChildByName(u))) {
                    var f;
                    (f = h.getComponent(r.default)) && l.push(f);
                }
            }
            o = {};
            for (d = 0; d < p.length; d++)
                for (var y = 0; y < l.length; y++) {
                    for (var v = [], g = p[d], m = l[y], _ = g.upataPos(), C = 0; C < _.length; C++)
                        v.push(_[_.length - C - 1]);
                    var b = _[_.length - 1];
                    if ((L = this.getMinPos(a, b)) && L.minPos)
                        for (var I = a.length, w = 9999; I > 0; ) {
                            (G = L.indexData - 1) < 0 && (G = a.length - 1);
                            var D = L.indexData + 1;
                            D %= a.length;
                            I--;
                            if (!a[G] || !a[D]) {
                                I = 0;
                                break;
                            }
                            var k = a[G].sub(b).mag(),
                                O = a[D].sub(b).mag();
                            if (!(k < w && O < w)) {
                                I = 0;
                                break;
                            }
                            if (k < O) {
                                w = k;
                                L.indexData = G;
                                v.push(this.getV2(cc.v2(b[L.indexData])));
                            } else {
                                w = O;
                                L.indexData = D;
                                v.push(this.getV2(cc.v2(b[L.indexData])));
                            }
                        }
                    console.log("第二数据 送往顾客数据接收");
                    var P = m.upataPos();
                    for (C = 0; C < P.length; C++) v.push(P[C]);
                    var V = "path" + (d + 1) + "_" + (y + 1);
                    console.log("第二数据 送往顾客数据接收 完成");
                    o[V] = v;
                }
        }
        console.log("主路线进行断点 npcRoad");
        var R = this.getMainNpcPath();
        console.log("主路线断点完成");
        console.log("顾客进入进行断点 roadIn");
        var S = this.getMainInPath();
        console.log("顾客进入完成");
        console.log("顾客退出进行断点 roadout");
        var M = this.getMainOutPath();
        console.log("顾客退出进行完成");
        for (d = 0; d < this.devCount; d++)
            for (var A = 0; A < this.tabCount; A++) {
                var T = i[d];
                if (!T) {
                    console.log("EditAnimailPath ----------------- devCom", T);
                    return;
                }
                var L,
                    B = T.upataPos(),
                    E = B[0],
                    N = n[A];
                if (!N) {
                    console.log("EditAnimailPath ----------------- ", N);
                    return;
                }
                var x = (K = N.upataPos())[0],
                    j = [],
                    U = B.length;
                for (C = 0; C < U; C++) j.push(this.getV2(cc.v2(B[U - 1 - C])));
                if ((L = this.getMinPos(R, E)) && L.minPos)
                    for (I = R.length, w = 9999; I > 0; ) {
                        var G;
                        (G = L.indexData - 1) < 0 && (G = R.length - 1);
                        D = L.indexData + 1;
                        D %= R.length;
                        I--;
                        if (!R[G] || !R[D]) {
                            I = 0;
                            break;
                        }
                        (k = R[G].sub(x).mag()), (O = R[D].sub(x).mag());
                        if (!(k < w && O < w)) {
                            I = 0;
                            break;
                        }
                        if (k < O) {
                            w = k;
                            L.indexData = G;
                            j.push(this.getV2(cc.v2(R[L.indexData])));
                        } else {
                            w = O;
                            L.indexData = D;
                            j.push(this.getV2(cc.v2(R[L.indexData])));
                        }
                    }
                var F = K.length;
                for (C = 0; C < F; C++) j.push(this.getV2(cc.v2(K[C])));
                t[(V = "npcRoad" + (d + 1) + "_" + (A + 1))] = j;
            }
        for (y = 0; y < this.tabCount; y++) {
            j = [];
            var K,
                q = (K = s[y].upataPos())[0],
                W = S.length,
                Q = 9999,
                Z = !1;
            for (d = 0; d < W; d++) {
                var H = d;
                if (!Z) {
                    if ((Y = S[H].sub(q).mag()) > Q) {
                        Z = !0;
                        break;
                    }
                    Q = Y;
                    j.push(this.getV2(cc.v2(S[H])));
                }
            }
            for (d = 0; d < K.length; d++) j.push(this.getV2(cc.v2(K[d])));
            e[(V = "road" + (y + 1) + "_0")] = j;
        }
        for (y = 0; y < this.tabCount; y++) {
            j = [];
            var z = c[y].upataPos(),
                $ = ((q = z[0]), M.length),
                J = ((Q = 9999), !1);
            for (d = 0; d < z.length; d++) {
                H = z.length - 1 - d;
                j.push(this.getV2(cc.v2(z[H])));
            }
            for (d = 0; d < $; d++) {
                H = d;
                if (J) j.push(this.getV2(cc.v2(M[H])));
                else {
                    var Y;
                    (Y = M[H].sub(q).mag()) > Q ? (J = !0) : (Q = Y);
                }
            }
            e[(V = "road" + (y + 1) + "_1")] = j;
        }
        this.getInflectionPoint();
        return {road: e, npc: t, centerData: o, inflection: R};
    };
    e.prototype.getMinPos = function (t, e) {
        for (var o = null, a = 999, i = 0, n = 0, r = 0; r < t.length; r++)
            if ((i = t[r].sub(e).mag()) < a) {
                a = i;
                n = r;
                o = cc.v2(t[r]);
            }
        return {minPos: o, indexData: n, minDis: a};
    };
    e.prototype.getMainNpcPath = function () {
        return this.getAnimalPath(this.animalClip, "npcRoad");
    };
    e.prototype.getwaitCookNpcPath = function () {
        return this.getAnimalPath(this.animalClip, "waitCook");
    };
    e.prototype.getMainInPath = function () {
        return this.getAnimalPath(this.animalClip, "roadIn");
    };
    e.prototype.getNpcPath2Path = function () {
        console.log("EditAnimailPath ----------------- getNpcPath2Path");
        return this.getAnimalPath(this.animalClip, "npcPath2");
    };
    e.prototype.getMainOutPath = function () {
        return this.getAnimalPath(this.animalClip, "roadout");
    };
    e.prototype.getCopyData = function (t) {
        var e = {};
        for (var o in t) e[o] = t[o];
        return e;
    };
    e.prototype.getSaveData = function () {
        var t = {},
            e = this.saveMap.json;
        t.ver = e.ver;
        t.tip = e.tip;
        t.data = this.getCopyData(e.data);
        var o = this.getParseData();
        console.log("EditorCom ----------------- 地图数据", o);
        t.data[this.lvIndex] = o;
        t.data[this.lvIndex];
        this.saveMap.json = t;
        return t;
    };
    e.prototype.save = function () {
        var t = this.getSaveData();
        console.log("EditorCom ----------------- ", t);
        h.saveFileInAssets("/Res/map/", "map001.json", JSON.stringify(t));
    };
    e.prototype.getV2 = function (t) {
        t.x = Math.floor(100 * t.x) / 100;
        t.y = Math.floor(100 * t.y) / 100;
        return {x: t.x, y: t.y};
    };
    n([p(cc.Integer)], e.prototype, "lvIndex", void 0);
    n([p(cc.Integer)], e.prototype, "tabCount", void 0);
    n([p(cc.Integer)], e.prototype, "devCount", void 0);
    n([p(cc.Integer)], e.prototype, "tabA", void 0);
    n([p(cc.Integer)], e.prototype, "tabB", void 0);
    n([p(cc.JsonAsset)], e.prototype, "saveMap", void 0);
    n([p(cc.AnimationClip)], e.prototype, "animalClip", void 0);
    n([p({displayName: "存储"})], e.prototype, "saveData", null);
    return n([c, l], e);
})(cc.Component);
o.default = u;
var h = (function () {
    function t() {}
    t.saveFile = function () {};
    t.saveFileInAssets = function (t, e, o) {
        this.saveFile("assets/" + t, e, o);
    };
    return t;
})();
